﻿using LandManager.Web.Models;
using LandManager.Web.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LandManager.Web.Controllers
{
    public class AgencyController : Controller
    {
        //
        // GET: /Agency/
        AgencyService agencyService = new AgencyService();
        public ActionResult Index()
        {
            var list = agencyService.GetList();

            ViewBag.Menu = "系统管理";
            if (list == null || list.Count < 1)
                return RedirectToAction("Create");

            return View(list);
        }

        public ActionResult Create()
        {
            ViewBag.Menu = "系统管理";
            return View();
        }

        //
        // POST: /Agency/Create

        [HttpPost]
        public ActionResult Create(Agency model)
        {
            try
            {
                var result = agencyService.Add(model);
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Edit(int id)
        {
            var agency = agencyService.FindByID(id);
            ViewBag.Menu = "系统管理";
            return View(agency);
        }

        //
        // POST: /Agency/Edit/5

        [HttpPost]
        public ActionResult Edit(Agency model)
        {
            try
            {
                var result = agencyService.Edit(model);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Agency/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Agency/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
